<template>
  <div class='container'>
    <data-view
      propViewKey='purchase_order_package'
      propViewForm='purchase/package/detail.vue'
      :propFlowKey='propFlowKey'
      ref='dataview'
      :propPreQueryWrapper='cpChildViewQueryWrapper'
      @after-load='afterLoad'
      @extra-button-click='extraButtonClick'
    />
  </div>
</template>

<script>

export default {
  name: 'package_order',
  data() {
    return {}
  },
  computed: {
    cpChildViewQueryWrapper() {
      let wrapper = this.$newWrapper().eq('purchaseType', 'package')
      return wrapper
    }
  },
  methods: {
    extraButtonClick(para) {
      let buttonKey = para.button.key
      if (buttonKey === 'submit') {
        // 审核通过
        this.submitOrder(para.data)
        return
      }
      if (buttonKey === 'pass') {
        // 审核通过
        this.passOrder(para.data)
        return
      }
      if (buttonKey === 'closeOrder') { //关闭订单
        this.closeOrder(para.data)
      }
      if (buttonKey === 'pushOrder') { //待推送订单导出
        this.pushOrder(para.data)
      }
    },
    submitOrder(data) {
      if (!data || data.length === 0) {
        this.utilMessage.error('请先选中订单')
        return
      }
      let ids = data.map(row => row.id)
      let length = data.filter(row => row.flowStatus === 'start').length
      if (length !== data.length) {
        this.utilMessage.error('只有制单中的订单才允许提交审核')
        return
      }

      this.utilVue.loadShow(true)
      let params = []
      for (let j = 0; j < ids.length; j += 1) {
        let param = {
          busId: ids[j],
          buttonKey: 'submit',
          flowKey: this.propFlowKey
        }
        params.push(param)
      }
      this.$ajax.post('/purchase/order/flow_change_list', params).then(resp => {
        this.utilVue.loadHide()
        if (resp.success) {
          this.utilMessage.success(resp.msg)
          this.$refs.dataview.ocRefresh()
        }
      })
    },
    pushOrder(data) {
      if (!data || data.length === 0) {
        this.utilMessage.error('请先选中订单')
        return
      }
      let ids = data.map(row => row.id)
      let statusLength = data.filter(row => row.flowStatus === 'end').length
      if (statusLength !== data.length) {
        this.utilMessage.error('只有审核通过的订单才允许推送导出')
        return
      }
      let pushedLength = data.filter(row => row.flagPushed === '0').length
      if (pushedLength !== data.length) {
        this.utilMessage.error('已经导出的订单不能重复导出')
        return
      }
      let params = { idList: ids }
      this.$ajax.post('/purchase/order/pushOrder', params).then(resp => {
        resp.success && this.$XModal.message({
          message: '导出成功，开始下载',
          status: 'success'
        }) && global.utils.downloadContextRes(resp.data)
        this.$refs.dataview.ocRefresh()
      })
    },
    passOrder(data) {
      if (!data || data.length === 0) {
        this.utilMessage.error('请先选中订单')
        return
      }
      let ids = data.map(row => row.id)
      let length = data.filter(row => row.flowStatus === 'submit').length
      if (length !== data.length) {
        this.utilMessage.error('只有已提交的订单才允许审核通过')
        return
      }
      this.utilVue.loadShow(true)
      let params = ids.map(row => {
        return {
          busId: row,
          buttonKey: 'pass',
          flowKey: this.propFlowKey
        }
      })
      this.$ajax.post('/purchase/order/flow_change_list', params).then(resp => {
        resp.success && this.utilMessage.success(resp.msg)
        this.$refs.dataview.ocRefresh()
        this.utilVue.loadHide()
      })
    },
    closeOrder(data) {
      if (!data || data.length === 0) {
        this.utilMessage.error('请先选中订单')
        return
      }
      let length = data.filter(row => !row.flagClosed).length
      if (length !== data.length) {
        this.utilMessage.error('只有未关闭的订单才允许关闭')
        return
      }
      this.utilVue.loadShow(true)
      //获取订单状态
      let codes = []
      data.forEach(item => {
        const { busKey } = item
        codes.push(busKey)
      })
      this.$ajax.post('/purchase/order/orderStatus', codes).then(response => {
        this.utilVue.loadHide()
        let resp = response.data
        let ids = data.map(row => row.id)
        let flag = this.parameters.allowCloseAfterDelivered()
        let flagAllowDeliveryClose = 0
        if (flag) {
          flagAllowDeliveryClose = 1
        }
        if (JSON.stringify(resp) !== '{}') {
          if (flag) {
            this.$confirm(resp, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$prompt('请输入关闭原因', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /.+/,
                inputErrorMessage: '关闭原因不能为空'
              }).then(({ value }) => {
                this.utilVue.loadShow(true)
                this.$ajax.post('/purchase/order/closeOrder', {
                  idList: ids,
                  reason: value,
                  flagDeliveryClose: flagAllowDeliveryClose,
                  user: this.utils.getCurrentUser().name
                }).then(resp => {
                  if (resp.code === 0) {
                    this.utilMessage.success(resp.msg)
                    this.$refs.dataview.ocRefresh()
                    this.utilVue.loadHide()
                  }
                  this.$refs.dataview.ocRefresh()
                })
              }).catch(() => {
              })
            }).catch(() => {
              this.utilMessage.info('已取消')
            })
          } else {
            this.utilMessage.error(resp + '请取消关闭')
          }
        } else {
          this.$prompt('请输入关闭原因', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /.+/,
            inputErrorMessage: '关闭原因不能为空'
          }).then(({ value }) => {
            this.utilVue.loadShow(true)
            this.$ajax.post('/purchase/order/closeOrder', {
              idList: ids,
              reason: value,
              flagDeliveryClose: flagAllowDeliveryClose,
              user: this.utils.getCurrentUser().name
            }).then(resp => {
              if (resp.code === 0) {
                this.utilMessage.success(resp.msg)
                this.$refs.dataview.ocRefresh()
                this.utilVue.loadHide()
              }
              this.$refs.dataview.ocRefresh()
            })
          }).catch(() => {
          })
        }
      })
    },
    afterLoad() {
      let flowNodeValue = this.$refs.dataview.flowNodeValue
      this.$refs.dataview.setButtonInvisible('view', 'submit', true)
      this.$refs.dataview.setButtonInvisible('view', 'pass', true)
      if (flowNodeValue == null || flowNodeValue === '' || flowNodeValue === undefined || flowNodeValue === 'submit') {
        this.$refs.dataview.setButtonInvisible('view', 'pass', false)
      }
      if (flowNodeValue == null || flowNodeValue === '' || flowNodeValue === undefined || flowNodeValue === 'start') {
        this.$refs.dataview.setButtonInvisible('view', 'submit', false)
      }
      if (this.parameters.allowInternet()) {
        this.$refs.dataview.setButtonInvisible('view', 'pushOrder', true)
      } else {
        this.$refs.dataview.setButtonInvisible('view', 'pushOrder', false)
      }
    }
  },
  props: ['propFlowKey'],
  activated() {
    let params = this.$route.params
    let autoQuery = 'autoQuery'
    if (params[autoQuery] != null && params[autoQuery] === true) {
      this.$refs.dataView.fetchData()
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  height: 100%;
  background-color: #fff;
}
</style>
